﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HouseHoldingRunTests.cs" company="DST Nexdox">
//   Copyright (c) DST Nexdox. All rights reserved.
// </copyright>
// <summary>
//   Defines the HouseHoldingRunTests type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceTests
{
  using System;
  using System.Collections.Generic;
  using BusinessEngineInterfaces;
  using Entities;
  using Exceptions;
  using FluentAssertions;
  using Moq;
  using NUnit.Framework;
  using ServiceInterfaces;
  using Services;
  using UnityRepository.Interfaces;

  [Category("Unit")]
  [TestFixture]
  public class HouseHoldingRunTests
  {
    private Mock<IHouseHoldingRunRepository> houseHoldingRunRepository;
    private Mock<IUserService> userService;
    private IHouseHoldingRunService houseHoldingRunService;

    [SetUp]
    public void SetUp()
    {
      this.houseHoldingRunRepository = new Mock<IHouseHoldingRunRepository>();
      this.userService = new Mock<IUserService>();

      this.houseHoldingRunService = new HouseHoldingRunService(
        this.houseHoldingRunRepository.Object,
        this.userService.Object);
    }

    [Test]
    public void GivenValidData_WhenIAskForRecentlyHouseHeldGrids_AndTheDataBaseIsNotAvailable_ThenAnUnityExceptionIsThrown()
    {
      this.houseHoldingRunRepository.Setup(p => p.GetTopFifteenRecentHouseHeldGrids(It.IsAny<List<Int32>>())).Throws<Exception>();

      Action act = () => this.houseHoldingRunService.GetTopFifteenRecentHouseHeldGrids(It.IsAny<List<Int32>>());

      act.ShouldThrow<UnityException>();
    }

    [Test]
    public void GivenValidData_WhenIAskForUnapprovedGrids_ThenTheGridsAreReturned()
    {
      this.houseHoldingRunRepository.Setup(p => p.GetTopFifteenRecentHouseHeldGrids(It.IsAny<List<Int32>>()))
        .Returns(new List<HouseHoldingRun> { new HouseHoldingRun() });

      var grids = this.houseHoldingRunService.GetTopFifteenRecentHouseHeldGrids(It.IsAny<List<Int32>>());
      grids.Count.Should().Be(1);
    }



    [Test]
    public void GivenValidData_WhenICreateANewHouseHoldingRun__DataIsSavedToTheDatabase()
    {
      this.houseHoldingRunService.CreateHouseHoldingRun(
        "HHRunGridId",
        DateTime.Now - TimeSpan.FromDays(1),
        DateTime.Now);

      this.houseHoldingRunRepository.Verify(hhr => hhr.Create(It.IsAny<HouseHoldingRun>()), Times.Once());
    }

    [Test]
    public void GivenValidData_WhenICreateANewHouseHoldingRun_AndTheDataBaseIsNotAvailable_ThenAnUnityExceptionIsThrown()
    {
      this.houseHoldingRunRepository.Setup(hhrr => hhrr.Create(It.IsAny<HouseHoldingRun>())).Throws<Exception>();

      Action act =
        () =>
        this.houseHoldingRunService.CreateHouseHoldingRun(
          "HHRunGridId",
          DateTime.Now - TimeSpan.FromDays(1),
          DateTime.Now);

      act.ShouldThrow<UnityException>();
    }
  }
}
